﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KpiConfigProject.Entity;
using KpiConfigProject.Manager;
using System.Reflection;
using KpiConfigProject.TypeEditor.Controls;

namespace KpiConfigProject.ChildForm
{
    public partial class KpiInfoConfigForm : Form
    {
        public KpiInfoConfigForm()
        {
            InitializeComponent();
        }

        private void KpiInfoConfigForm_Load(object sender, EventArgs e)
        {
            InitKpiInfo();
        }

        private void InitKpiInfo()
        {
            KpiInfoManager kpiManger = new KpiInfoManager();
            DataTable table = kpiManger.GetList();
            if (table != null)
                DGV_KpiInfo.DataSource = table.DefaultView;
        }

        /// <summary>
        /// 点击单元格时， 显示当行的详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGV_KpiInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex <= -1 || e.ColumnIndex <= -1)
            {
                PG_KpiInfo.SelectedObject = null;
                return;
            }
            if (tabControl1.Tag != null && tabControl1.Tag.ToString() == e.RowIndex.ToString())
                return;
            DataGridViewRow selectRow = DGV_KpiInfo.Rows[e.RowIndex];
            DataGridViewColumnCollection columns = DGV_KpiInfo.Columns;
            KpiInfo kpiInfo = new KpiInfo();
            Type kpiType = kpiInfo.GetType();
            foreach (DataGridViewColumn column in columns)
            {
                string columnText = column.HeaderText;
                object value = selectRow.Cells[columnText].Value;
                if (value.ToString() != "")
                    kpiType.GetProperty(columnText).SetValue(kpiInfo, value, null);
            }
            PG_KpiInfo.SelectedObject = kpiInfo;

        }

        private void tsb_Save_Click(object sender, EventArgs e)
        {
            KpiInfoManager manager = new KpiInfoManager();
            bool isSucc = manager.InsertData(DGV_KpiInfo.AddRows, DGV_KpiInfo.ModRows, DGV_KpiInfo.DelRows);
            if (isSucc)
            {
                DGV_KpiInfo.ClearRowData();
                MessageBox.Show("修改数据成功！");
            }
        }

        private void tsb_Add_Click(object sender, EventArgs e)
        {
            DGV_KpiInfo.AddNewRow();
        }

        private void tsb_Delete_Click(object sender, EventArgs e)
        {
            DGV_KpiInfo.DeleteRows();
        }

        private void tsb_Reduction_Click(object sender, EventArgs e)
        {
            DGV_KpiInfo.RestoreRows();
        }

        private void tsb_Refresh_Click(object sender, EventArgs e)
        {
            InitKpiInfo();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PG_KpiInfo_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
        {
            if (DGV_KpiInfo.SelectedCells.Count <= 0)
                return;
            int rowIndex = DGV_KpiInfo.SelectedCells[0].RowIndex;
            DataGridViewRow selectRow = DGV_KpiInfo.Rows[rowIndex];
            if (e.NewSelection != null && DGV_KpiInfo.Columns.Contains(e.NewSelection.Label))
                DGV_KpiInfo.Rows[rowIndex].Cells[e.NewSelection.Label].Value = e.NewSelection.Value;
            if (e.OldSelection != null && DGV_KpiInfo.Columns.Contains(e.OldSelection.Label))
                DGV_KpiInfo.Rows[rowIndex].Cells[e.OldSelection.Label].Value = e.OldSelection.Value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGV_KpiInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex <= -1 || e.ColumnIndex <= -1)
                return;
            string columnName = DGV_KpiInfo.Columns[e.ColumnIndex].HeaderText;
            DataGridViewCell clickCell = DGV_KpiInfo.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (columnName == "")
            {
                string formula = clickCell.Value.ToString();
                using (FormulaEditorCtrl frm = new FormulaEditorCtrl())
                {
                    frm.FormulaString = formula;
                    frm.ShowDialog();
                    formula = frm.FormulaString;
                }
                clickCell.Value = formula;
            }
        }

        private void DGV_KpiInfo_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex <= -1 || e.ColumnIndex <= -1)
                return;
            string columnName = DGV_KpiInfo.Columns[e.ColumnIndex].HeaderText;
            DataGridViewRow selectRow = DGV_KpiInfo.Rows[e.RowIndex];
            if (columnName == "vcFormula")
            {
                using(FormulaEditorCtrl frm = new FormulaEditorCtrl())
                {
                    frm.FormulaString = selectRow.Cells[e.ColumnIndex].Value.ToString();
                    frm.ShowDialog();
                    selectRow.Cells[e.ColumnIndex].Value = frm.FormulaString;
                }
            }
        }

    }
}
